#!/usr/bin/env python
import sys
sys.path.append("/work/opt/analytic/my_python/lib")
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from lib.email_helper import *
from jpype import *
from lib.load_data import *
import urllib
import json
import glob
import sys
import re
import os
sys.setdefaultencoding('utf-8')


class Service58(object):
    def __init__(self):
        self.jars = ':'.join(glob.glob('/work/opt/analytic/my_python/java_lib/imc/*.jar'))
        self.jvm_path = '/opt/soft/java/jre/lib/amd64/server/libjvm.so'
        startJVM(self.jvm_path,"-ea","-Djava.class.path=%s" % self.jars)
        self.SCFInit =JClass("com.bj58.spat.scf.client.SCFInit")
        self.SCFInit.init('/work/opt/analytic/my_python/config/scf.config')
        
        URL= "tcp://imc/InfoProviderWithIndex"            
        self.ProxyFactory =JClass("com.bj58.spat.scf.client.proxy.builder.ProxyFactory")
        #self.IProductService =JClass("com.bj58.sfft.imc.cntity.Info")
        self.Info =JClass("com.bj58.sfft.imc.contract.IInfoService")
        self.Integer =JClass("java.lang.Integer")
        self.proxy = self.ProxyFactory.create(self.Info,URL)
        #self.out = JClass("com.bj58.spat.scf.server.contract.entity.Out")
        #self.out_total = self.out{}
        print "ok"
        
    def get_info(self,info_id): #
        self.infoID = long(info_id)
        #print self.infoID
        self.info_model = self.proxy.GetInfo(self.infoID,"infoid,cateid,locals,params")
        
        return self.info_model
        
    def shut_down(self):
        shutdownJVM()

#mysql - dict
def set_dict_conn():    
    conn = HandleMysql('10.5.20.16','58885','ec_stat','L#c8UM5RGh5C','dbwww58com_base_dicts')
    conn.connect()
    return conn

#mysql - dict
def set_wb_conn():    
    conn = HandleMysql('10.5.20.16','58885','ec_stat','L#c8UM5RGh5C','dbwww58com_58_stats')
    conn.connect()
    return conn

#mysql - infobright
def set_ib_conn():    
    conn = HandleMysql('10.5.20.15','5029','root','group_buytj1140','zhaopin')
    conn.connect()
    return conn

def get_resume_info(date,s58):
    dict_conn = set_dict_conn()
    wb_conn = set_wb_conn()    
    date_str = date.replace('-',"")    
    url_str = "http://10.5.12.160:50075/streamFile/dsap/rawdata/t_zpt_userdelivery/%s/t_zpt_userdelivery.dat"%date_str
    file_path = "/work2/opt/analytic/lh_tmp_data/zp_info/resume_%s.txt"%date_str
    
    #print url_str,file_path    
    urllib.urlretrieve(url_str, file_path)
    line_list = []        
    stat_table = "zp_user_delivery_info"
        
    f = open(file_path)
    for line in f.readlines():
        #599961556656	2575252463879	12219580134153	66057969475331	0	0	0	51447596
        #print line
        arr = line.split('\t')
        #print arr
        user_id = arr[1]
        info_id = arr[2]
        #print info_id
        info = None
        try:
            info = s58.get_info(info_id)
        except Exception ,ex:
            pass
        #print info
        params = ''
        cate_id = 0
        local_str = ''
        if info == None:
            continue
        
        #print line
        params = info.getParams()
        local_str= info.getLocals()
        cate_id = info.getCateID() 

        #print cate_id
        if local_str == '':
            continue
        if cate_id == 0 :
            continue
        
        dc = get_thirth_cat(cate_id,params,dict_conn)    
        cat1 = 0
        cat1name = ''
        cat2 = 0
        cat2name=''
        cat3 = 0
        cat3name=''
        if dc != None:
            if len(dc) > 0:
                cat1 = dc[0][0]
                cat1name = dc[0][1]
            if len(dc) > 1:
                cat2 = dc[1][0]
                cat2name=dc[1][1]
            if len(dc) > 2:
                cat3 = dc[2][0]
                cat3name=dc[2][1]
        
        cate_name = ''
        cate2_id = 0
        cate2_name = ''
        dc = get_cates(cate_id,dict_conn)
        if dc != None:
            if len(dc) > 0:
                cate_name = dc[0][1]
            if len(dc) > 1:
                cate2_id = dc[1][0]
                cate2_name=dc[1][1]
                
        city = json.loads(local_str)[0]['D']
        #print city
        city_id = 0
        city_name = ''
        area_id = 0
        area_name = ''
        local_id= 0
        local_name = ''
        
        dc = get_citys(city,dict_conn)
        if dc != None:
            if len(dc) >0:
                city_id = dc[0][0]
                city_name = dc[0][1]
            if len(dc)>1:
                area_id = dc[1][0]
                area_name = dc[1][1]
            if len(dc)>2:
                local_id= dc[2][0]
                local_name = dc[2][1]
        
        param = [user_id,info_id,city_id,city_name,area_id,area_name,local_id,local_name,cat1,cat1name,cat2,cat2name,cat3,cat3name,cate_id,cate_name,cate2_id,cate2_name,date]
        line_list.append('\t'.join(map(lambda i : str(i), param)))
    
    cont = '\n'.join(line_list)
    cont = cont.strip()
    path = "/work2/opt/analytic/lh_tmp_data/zp_info/data/"
    file_name = "resume_info_%s.txt"%date.replace('-',"")
    writeTextToFile(path,file_name,"w",cont)
    
    load_data(wb_conn,path+file_name,stat_table,'\t')
    
    
    dict_conn.close()

    #send to ib
    ib_conn = set_ib_conn()    
    #print url_str,file_path    
    line_list = []        
    #stat_table = "zp_user_delivery_info"    
    query = "select user_id,info_id,city_id,city_name,area_id,area_name,local_id,local_name,disp_cate1_id,disp_cate1_name,disp_cate2_id,disp_cate2_name,disp_cate3_id,disp_cate3_name,cate1_id,cate1_name,cate2_id,cate2_name from zp_user_delivery_info where stat_date='%s'"%date
    items = wb_conn.get_results(query)
    for item in items:
        user_id = item[0]
        info_id = item[1]
        city_id= item[2]
        city_name= item[3]
        area_id= item[4]
        area_name= item[5]
        local_id= item[6]
        local_name= item[7]
        disp_cate1_id= item[8]
        disp_cate1_name= item[9]
        disp_cate2_id= item[10]
        disp_cate2_name= item[11]
        disp_cate3_id= item[12]
        disp_cate3_name= item[13]
        cate1_id= item[14]
        cate1_name= item[15]
        cate2_id= item[16]
        cate2_name= item[17]
        
        param = [user_id,info_id,city_id,city_name,area_id,area_name,local_id,local_name,disp_cate1_id,disp_cate1_name,disp_cate2_id,disp_cate2_name,disp_cate3_id,disp_cate3_name,cate1_id,cate1_name,cate2_id,cate2_name]
        line_list.append('\t'.join(map(lambda i : str(i), param)))
    
    cont = '\n'.join(line_list)
    cont = cont + '\n'
    #path = "/work2/opt/analytic/lh_tmp_data/zp_info/data/"
    file_name = "resume_dictinfo_%s.txt"%date.replace('-',"")
    writeTextToFile(path,file_name,"w",cont)
    
    load_data(ib_conn,path+file_name,stat_table,'\t')
    
    ib_conn.close()
    wb_conn.close()
def get_citys(city_id,conn):
    if city_id == 0 :
        return None

    query = 'select fullpath from t_displocal where displocalid=%s' % city_id
    items = conn.get_results(query)
    fullpath = None
    if len(items)>0:
        fullpath = items[0][0]
    
    if fullpath is None:
        return None

    return get_citys_by_fullpath(fullpath,conn)

def get_citys_by_fullpath(fullpath,conn):
    if fullpath == "":
        return None
    
    query = "select displocalid, localname from t_displocal where displocalid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query)

def get_cates(cate_id,conn):
    if cate_id == 0 :
        return None

    query = 'select fullpath from t_category where cateid=%s' % cate_id
    items = conn.get_results(query)
    fullpath = None
    if len(items)>0:
        fullpath = items[0][0]
    
    if fullpath is None:
        return None
    
    query = "select cateid, catename from t_category where cateid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query)
    

def get_thirth_cat(cate_id ,param,conn):    
    #print param
    param_id = 0
    query = 'select param_id from t_cate_param where cate_id = %s;' % cate_id
    items = conn.get_results(query)
    if len(items) > 0:
        param_id = items[0][0]
        
    #print param_id
    if param_id == 0:
        return get_second_cat(cate_id ,conn)
    
    reg = '{"I":%s,"V":"(\w*)"}'%param_id
    #print reg
    pattern =  re.compile(reg)
    matchs = pattern.search(param)
    #print reg
    objval = "0"
    #print matchs
    try:
        objval = matchs.group(1)
    except Exception,e:
        return get_second_cat(cate_id,conn )
        
    filter_str = "objecttype=%s"%objval
    #print filter_str
    fullpath = ''
    query = "select fullpath from t_dispcategory where cateid =%s and filter='%s'" % (cate_id,filter_str)
    items = conn.get_results(query)
    if len(items) > 0 :
        fullpath = items[0][0]
    #print fullpath
    if fullpath == '':
        return get_second_cat(cate_id ,conn)
    
    query = "select dispcategoryid, catename from t_dispcategory where dispcategoryid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query )
    
def get_second_cat(cate_id,conn ):    
    fullpath = ''
    query = 'select fullpath from t_dispcategory where cateid =%s and depth=1;' % (cate_id)
    items = conn.get_results(query)
    if len(items) > 0 :
        fullpath = items[0][0]
    #print fullpath
    if fullpath == '':
        query = "select cateid, catename from t_category where cateid=%s" % cate_id
        return conn.get_results(query)
    return get_second_cat_by_full_path(fullpath,conn)
    
def get_second_cat_by_full_path(fullpath,conn ):
    if fullpath == "":
        return None
    query = "select dispcategoryid, catename from t_dispcategory where dispcategoryid in (%s) order by depth asc" % fullpath
    return conn.get_results(query )
    
def get_page_view(date):
    dict_conn = set_dict_conn()
    ib_conn = set_ib_conn()    
    date_str = date.replace('-',"")
    line_list = []   
    pattern =  re.compile(r'(\w*)')
    stat_table = "zp_info_pv_stats"
    
    for i in range(5):
        url_str = "http://10.5.12.160:50075/streamFile/dsap/resultdata/ec_data/EC_ZP_PVJob/%s/part-r-0000%s"%(date_str,i)
        file_path = "/work2/opt/analytic/lh_tmp_data/zp_pvinfo/info_%s_%s.txt"%(date_str,i)
        
        #print url_str,file_path    
        urllib.urlretrieve(url_str, file_path)        
            
        f = open(file_path)
        for line in f.readlines():
            #info_http://ab.58.com/qzjisuanjiwl/11990646350338x.shtml?sd_count_1_params_{'cate':'13952,13610','area':'9817,9818','is_biz':'false','pagetype':'detail'}
            cont = line.replace('info_http://','')
            cont = cont.replace('_count_','|')
            cont = cont.replace('_params_','|')
            #print cont
            arr = cont.split('|')
            #print arr[0]
            info_id = ""
            try:
                url = arr[0].split('/')[2]
                info_id = url.split('x')[0]
            except Exception,e:
                continue
            
            pv = arr[1]
            #print arr
            pm = arr[2].replace('\n','')
            pm = pm.replace("'","\"")
            pms = None
            try:
                pms = json.loads(pm)
            except Exception,e:
                continue
            #print pms
            if pms == None:
                continue
            
            is_biz ='false'
            cate = pms['cate']
            area = pms['area']
            try:
                is_biz = pms['is_biz']
            except Exception,e:
                pass
            
            if is_biz =='false':
                is_biz = 0
            else:
                is_biz = 1
   
            dc = get_second_cat_by_full_path(cate,dict_conn)    
            cat1 = 0
            cat1name = ''
            cat2 = 0
            cat2name=''
            cat3 = 0
            cat3name=''
            if dc != None:
                if len(dc) > 0:
                    cat1 = dc[0][0]
                    cat1name = dc[0][1]
                if len(dc) > 1:
                    cat2 = dc[1][0]
                    cat2name=dc[1][1]
                if len(dc) > 2:
                    cat3 = dc[2][0]
                    cat3name=dc[2][1]
            
            city_id = 0
            city_name = ''
            area_id = 0
            area_name = ''
            local_id= 0
            local_name = ''
            
            dc = get_citys_by_fullpath(area,dict_conn)
            if dc != None:
                if len(dc) >0:
                    city_id = dc[0][0]
                    city_name = dc[0][1]
                if len(dc)>1:
                    area_id = dc[1][0]
                    area_name = dc[1][1]
                if len(dc)>2:
                    local_id= dc[2][0]
                    local_name = dc[2][1]
            
            param = [date,info_id,is_biz,city_id,city_name,area_id,area_name,local_id,local_name,cat1,cat1name,cat2,cat2name,cat3,cat3name,pv]
            line_list.append('\t'.join(map(lambda i : str(i), param)))
            remove_file(file_path)
    
    cont = '\n'.join(line_list)
    cont = cont + '\n'
    path = "/work/opt/analytic/lh_tmp_data/zp_pvinfo/data/"
    file_name = "resume_info_pv_%s.txt"%date_str
    writeTextToFile(path,file_name,"w",cont)
    
    load_data(ib_conn,path+file_name,stat_table,'\t')
    #remove_file(path+file_name)
    
    ib_conn.close()
    dict_conn.close()
                
def execute(date,s58):
    #get_page_view(date)
    try:
        pass
        get_resume_info( date,s58 )
    except Exception,e:
        print 'get_resume_info error:',e
    try:
        pass
        get_page_view( date )
    except Exception,e:
        print 'get_page_view error:',e
        
def main():

   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])

   stat_date=start_date
   s58=Service58()
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())

         #process data
        execute( stat_date,s58) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)

if __name__=='__main__':
    main()
    
    #ib_conn = set_ib_conn()
    #path = "/work/opt/analytic/lh_tmp_data/zp_pvinfo/data/"
    #file_name = "resume_info_pv_20130110.txt"

    #load_data(ib_conn,path+file_name,"zp_info_pv_stats",'\t')    
    #ib_conn.close()
    
   
